﻿/*************************************************************************
 *
 *    GNU Lesser General Public License Version 2.1
 *    =============================================
 *    Copyright 2005 by Sun Microsystems, Inc.
 *    901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License version 2.1, as published by the Free Software Foundation.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public
 *    License along with this library; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *    MA  02111-1307  USA
 *
 ************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using Newtonsoft.Json;
using CatchUpTv.Tv;

namespace CatchUpTv.Downloader.Australia.AbcIView
{
    /// <summary>
    /// An object for deserializing a series from iView's json.
    /// </summary>
    /// <remarks>Sample json: {"a":"3036240","b":"Arthur Series 1","d":"http:\/\/www.abc.net.au\/reslib\/201010\/r654525_4631688.jpg","e":"pre-school abc2 recent","f":[{"a":"654437","b":"Arthur Series 1 Episode 4","c":"D.W's Imaginary Friend\/Arthur's Lost Library Book","d":"Arthur, an 8-year-old aardvark, guides us through energetic, emotional stories about growing up. His world revolves around his family and friends and he lives in a town peopled by odd and engaging animals.","e":"ABC For Kids","f":"2010-10-20 16:05:00","g":"2010-11-03 16:05:00","h":"2010-10-22 00:00:00","i":"125","j":"1500","m":"G","n":"kids\/arthur_10_01_04.mp4","s":"http:\/\/www.abc.net.au\/reslib\/201010\/r654525_4631688.jpg","t":false},{"a":"654432","b":"Arthur Series 1 Episode 3","c":"D.W All Wet\/Buster's Dino Dilemma","d":"Arthur, an 8-year-old aardvark, guides us through energetic, emotional stories about growing up. His world revolves around his family and friends and he lives in a town peopled by odd and engaging animals.","e":"ABC For Kids","f":"2010-10-19 16:05:00","g":"2010-11-02 16:05:00","h":"2010-10-21 00:00:00","i":"125","j":"1500","m":"G","n":"kids\/arthur_10_01_03.mp4","s":"http:\/\/www.abc.net.au\/reslib\/201010\/r654525_4631688.jpg","t":false},{"a":"654428","b":"Arthur Series 1 Episode 2","c":"Arthur And The Real Mr Ratburn\/Arthur's Spelling","d":"Arthur, an 8-year-old aardvark, guides us through energetic, emotional stories about growing up. His world revolves around his family and friends and he lives in a town peopled by odd and engaging animals.","e":"ABC For Kids","f":"2010-10-18 16:05:00","g":"2010-11-01 16:05:00","h":"2010-10-20 00:00:00","i":"125","j":"1500","m":"G","n":"kids\/arthur_10_01_02.mp4","s":"http:\/\/www.abc.net.au\/reslib\/201010\/r654525_4631688.jpg","t":false},{"a":"654424","b":"Arthur Series 1 Episode 1","c":"Arthur's Eyes\/Francines Bad Hair Day","d":"Arthur, an 8-year-old aardvark, guides us through energetic, emotional stories about growing up. His world revolves around his family and friends and he lives in a town peopled by odd and engaging animals.","e":"ABC For Kids","f":"2010-10-17 16:05:00","g":"2010-10-31 16:05:00","h":"2010-10-19 00:00:00","i":"125","j":"1500","m":"G","n":"kids\/arthur_10_01_01.mp4","s":"http:\/\/www.abc.net.au\/reslib\/201010\/r654525_4631688.jpg","t":false}]}</remarks>
    [JsonObject(MemberSerialization = MemberSerialization.OptIn)]
    public class Series
    {
        /// <summary>
        /// The Id for the series.
        /// </summary>
        [JsonProperty(PropertyName = "a")]
        public int Id { get; set; }

        /// <summary>
        /// The series name.
        /// </summary>
        [JsonProperty(PropertyName = "b")]
        public string Name { get; set; }

        /// <summary>
        /// The series image URL.
        /// </summary>
        [JsonProperty(PropertyName = "d")]
        public string ImageUrl { get; set; }

        /// <summary>
        /// The series categories.
        /// </summary>
        [JsonProperty(PropertyName = "e")]
        public string Categories { get; set; }

        /// <summary>
        /// The series description.
        /// </summary>
        [JsonProperty(PropertyName = "f")]
        public IEnumerable<SeriesEpisode> Episodes { get; set; }

        /// <summary>
        /// Get the list of episodes for this series.
        /// </summary>
        /// <returns>The list of episodes</returns>
        public IEnumerable<Episode> GetEpisodes()
        {
            IList<Episode> episodeList = new List<Episode>();

            foreach (SeriesEpisode episode in Episodes)
            {
                episodeList.Add(new Episode()
                {
                    Id = "ABC_iView_episode_" + episode.Id,
                    Tag = episode,
                    Name = episode.Name + " " + episode.Title,
                    CaptionsAvailable = episode.CaptionsAvailable,
                    DateAdded = episode.Added,
                    DateExpires = episode.Expires,
                    Duration = TimeSpan.FromSeconds(episode.Duration),
                    FileSize = episode.FileSize,
                    Description = episode.Description,
                    RatingInformation = episode.Rating,
                    ImageUrl = episode.ImageUrl
                });
            }

            return episodeList;
        }
    }
}
